package com.addcn.caruimodule.list;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.addcn.caruimodule.R;


public class IndicatorHorizontalScrollView extends FrameLayout {
    private CustomHorizontalScrollView horizontalScrollView;
    private MyIndicator indicator;
    private boolean constructed = false;
    private int bgColor=0xFFBBBBBB,mProgressColor=0xFFFF963C;
    public IndicatorHorizontalScrollView(@NonNull Context context) {
        this(context, null);
        initView(context);
    }

    public IndicatorHorizontalScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IndicatorHorizontalScrollView);
        bgColor = a.getColor(R.styleable.IndicatorHorizontalScrollView_indicator_bg_color, Color.parseColor("#FFBBBBBB"));
        mProgressColor = a.getColor(R.styleable.IndicatorHorizontalScrollView_indicator_progress_color, Color.parseColor("#FFFF963C"));
        initView(context);
    }

    public IndicatorHorizontalScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IndicatorHorizontalScrollView);
        bgColor = a.getColor(R.styleable.IndicatorHorizontalScrollView_indicator_bg_color, Color.parseColor("#FFBBBBBB"));
        mProgressColor = a.getColor(R.styleable.IndicatorHorizontalScrollView_indicator_progress_color, Color.parseColor("#FFFF963C"));
        initView(context);

    }

    private void initView(Context context) {
        horizontalScrollView = new CustomHorizontalScrollView(context);
        horizontalScrollView.setOnScrollListener((nowScrollX, oldScrollX) -> {
            float position = 1f * nowScrollX / (horizontalScrollView.getChildAt(0).getRight() - horizontalScrollView.getWidth());
            indicator.setNowPosition(position);
        });
        addView(horizontalScrollView);

        indicator = new MyIndicator(context);
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, context.getResources().getDisplayMetrics());
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, context.getResources().getDisplayMetrics());
        LayoutParams layoutParams = new LayoutParams(width, height);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        layoutParams.bottomMargin = 20;
        addView(indicator, layoutParams);

        GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadius(height/2f);
        bg.setColor(bgColor);
        indicator.setBackground(bg);

        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(mProgressColor);
        drawable.setCornerRadius(height/2f);
        width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, context.getResources().getDisplayMetrics());
        height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, context.getResources().getDisplayMetrics());
        drawable.setSize(width, height);
        indicator.setImageDrawable(drawable);
        constructed=true;
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
        horizontalScrollView.setLayoutParams(generateLayoutParams(params));
    }

    @Override
    public void addView(View child) {
        if (getChildCount() >= 3) {
            throw new RuntimeException("only one child");
        }
        if (constructed) {
            horizontalScrollView.addView(child);
        } else {
            super.addView(child);
        }
    }

    @Override
    public void addView(View child, int index) {
        if (getChildCount() >= 3) {
            throw new RuntimeException("only one child");
        }
        if (constructed) {
            horizontalScrollView.addView(child, index);
        } else {
            super.addView(child, index);
        }
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        if (getChildCount() >= 3) {
            throw new RuntimeException("only one child");
        }
        if (constructed) {
            horizontalScrollView.addView(child, params);
        } else {
            super.addView(child, params);
        }
    }

    @Override
    public void addView(View child, int width, int height) {
        if (getChildCount() >= 3) {
            throw new RuntimeException("only one child");
        }
        if (constructed) {
            horizontalScrollView.addView(child, width, height);
        } else {
            super.addView(child, width, height);
        }
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() >= 3) {
            throw new RuntimeException("only one child");
        }
        if(constructed) {
            horizontalScrollView.addView(child, params);
        }else{
            super.addView(child,index,params);
        }
    }

    public void showIndicator(boolean bool){
        indicator.setVisibility(bool ? View.VISIBLE : View.GONE);
    }

    private static class MyIndicator extends AppCompatImageView {
        float position;
        Matrix matrix;

        public MyIndicator(Context context) {
            this(context, null);
        }

        public MyIndicator(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public MyIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            matrix = new Matrix();
            setScaleType(ScaleType.MATRIX);
        }

        void setNowPosition(float position) {
            this.position = position;
            invalidate();
        }

        float getNowPosition() {
            return position;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int drawableWidth = getDrawable().getIntrinsicWidth();
            int scrollable = getWidth() - drawableWidth;
            matrix.setTranslate(scrollable * position, 0);
            setImageMatrix(matrix);
            super.onDraw(canvas);
        }

    }
}
